package edu.indiana.d2i.app;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import edu.indiana.d2i.app.util.SolrTermFrequencyResult;

public class SimpleScript extends ScriptBase {
	private String absolutepath = null;
	private BufferedWriter out = null;
	
	private void openOutputFile() {
		UUID randomUUID = UUID.randomUUID();
		String filename = "out-" + randomUUID.toString() + ".txt";
		
		// Create file
		try {
			File file = new File(filename);
			FileWriter fstream = new FileWriter(file);
			out = new BufferedWriter(fstream);
			
			absolutepath = file.getAbsolutePath();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	private void closeOutputFile() {
		try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String makeSolrURL(String volumnID) {
		StringBuilder url = new StringBuilder();
		url.append(solrIndexURI);
		url.append("/select/?q=id%3A\"");
		url.append(volumnID);
		url.append("\"");
		url.append("&version=2.2&start=0&rows=10&qt=tvrh&tv.tf=true&tv.positions=true");

		return url.toString();
	}

	private void saveWordFrequency(
			List<HashMap<String, ArrayList<ArrayList<Integer>>>> wordList, String volumnID) {
		try {			
			// write to file
			out.write(volumnID + "\n");
			for (HashMap<String, ArrayList<ArrayList<Integer>>> hashMap : wordList) {
				for (String str : hashMap.keySet()) {
					StringBuilder strB = new StringBuilder();
					strB.append(str + "\t");
					strB.append(hashMap.get(str).get(0).get(0) + "\t");
					for (Integer offset : hashMap.get(str).get(1)) {
						strB.append(offset + "\t");
					}
//					System.out.print(str + "\t");
//					System.out.print(hashMap.get(str).get(0).get(0) + "\t");
//					for (Integer offset : hashMap.get(str).get(1)) {
//						System.out.print(offset + "\t");
//					}
//					System.out.println();
					
					out.write(strB.toString() + "\n");
				}
			}
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	public SimpleScript(String solrIndexURI, String repoURI,
			List<String> volumnIDs) {
		super(solrIndexURI, repoURI, volumnIDs);
	}

	@Override
	public void run() {
		// initialize the output file
		openOutputFile();
		
		// interact with solr index server
		for (String volumnID : volumnIDs) {
			String url = makeSolrURL(volumnID);
			
			System.out.println(url);
			
			// get term vector from solr index
			SolrTermFrequencyResult result = new SolrTermFrequencyResult(url,
					"w.*");
			List<HashMap<String, ArrayList<ArrayList<Integer>>>> wordList = result
					.getWordList();
			saveWordFrequency(wordList, volumnID);
		}
		
		closeOutputFile();
		
		System.out.println("***************************************************");
		System.out.println("final result is in: " + absolutepath);
		System.out.println("***************************************************");
	}

	/* test drive */
	public static void main(String[] args) {
		System.out.println("hello world");
	}
}
